package cn.edu.henu.team.MPChart;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.table.health_p;
import cn.edu.henu.team.table.healthpUtils;
import cn.edu.henu.team.utils.CalendarDialog;
import cn.edu.henu.team.utils.ChartUtil;


public class LineDetalActivity extends BaseActivity {
    private Intent mIntent;
    private int selectYear;
    private int getSelectMonth;
    private String kind;
    private ImageView back;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.chart_riLi)
    ImageView riLi;
    @BindView(R.id.result)
    TextView result;

    private List<health_p> data;
    private List<LineEntry> mLineEntry = new ArrayList<>();
    int selectPos = -1, selectMonth1 =-1;
    private float sum = 0;
    private int num = 0;
    private LineChart mLineChart;

    @Override
    protected int initLayout() {
        return R.layout.activity_line_detal;
    }

    @Override
    protected void initView() {
        kind = getIntent().getStringExtra("kind");
        selectYear = getIntent().getIntExtra("year", Calendar.getInstance().get(Calendar.YEAR));
        getSelectMonth = getIntent().getIntExtra("month",Calendar.getInstance().get(Calendar.MONTH));

        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        time.setText("当前日期: " + selectYear + " 年 " + getSelectMonth + "月");

        mLineChart = findViewById(R.id.lineChart);
        ChartUtil.initChart(mLineChart);  //生成单条折线图

    }

    @Override
    protected void initData() {
        data = healthpUtils.queryListByYearAndMonth(selectYear, getSelectMonth);

        for (health_p t : data) {
            if(kind.equals("龋病")){
                mLineEntry.add(new LineEntry(t.getDay(), t.getP1())); //获取一条曲线的数据
                sum+=t.getP1();
            }else if(kind.equals("牙髓炎")){
                mLineEntry.add(new LineEntry(t.getDay(), t.getP2())); //获取一条曲线的数据
                sum+=t.getP2();
            }else if(kind.equals("根尖周炎")){
                mLineEntry.add(new LineEntry(t.getDay(), t.getP3())); //获取一条曲线的数据
                sum+=t.getP3();
            }

            num++;
        }
        float avg = sum / num;
        result.setText(String.valueOf(avg));

        ChartUtil.showLineChart(mLineChart, mLineEntry, kind, R.color.green);
        Drawable drawable = getResources().getDrawable(R.drawable.fade_blue);
        ChartUtil.setChartFillDrawable(mLineChart, drawable);
        ChartUtil.setMarkerView(mLineChart, getApplicationContext());
    }

    @OnClick({R.id.chart_riLi})
    protected void OnClick(View v) {
        switch (v.getId()) {
            case R.id.chart_riLi:
                showCalendarDialog();
                break;
            default:
                break;
        }
    }

    /* 显示日历对话框*/
    private void showCalendarDialog() {
        CalendarDialog dialog = new CalendarDialog(this, selectPos, selectMonth1);
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnRefreshListener(new CalendarDialog.OnRefreshListener() {
            @Override
            public void onRefresh(int selPos, int year, int month) {
                data.clear();
                mLineEntry.clear();
                sum=0;
                num=0;
                data = healthpUtils.queryListByYearAndMonth(year, month);
                time.setText("当前日期: " + year + " 年 " + month + "月");

                for (health_p t : data) {
                    if(kind.equals("龋病")){
                        mLineEntry.add(new LineEntry(t.getDay(), t.getP1())); //获取一条曲线的数据
                        sum+=t.getP1();
                    }else if(kind.equals("牙髓炎")){
                        mLineEntry.add(new LineEntry(t.getDay(), t.getP2())); //获取一条曲线的数据
                        sum+=t.getP2();
                    }else if(kind.equals("根尖周炎")){
                        mLineEntry.add(new LineEntry(t.getDay(), t.getP3())); //获取一条曲线的数据
                        sum+=t.getP3();
                    }

                    num++;
                }
                float avg = sum / num;
                String text = String.format("%.2f",avg)+"%";
                result.setText(text);

                ChartUtil.resetLine(0, mLineChart, mLineEntry, kind, R.color.green);
                Drawable drawable = getResources().getDrawable(R.drawable.fade_blue);
                ChartUtil.setChartFillDrawable(mLineChart, drawable);
                ChartUtil.setMarkerView(mLineChart, getApplicationContext());
            }
        });
    }
}